`
https://leetcode.cn/problems/palindrome-linked-list/
`

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} head
 * @return {boolean}
 */
var isPalindrome = function (head) {
  let slow = head, fast = head
  // 快慢指针寻找中点
  while (fast && fast.next) {
    slow = slow.next
    fast = fast.next.next
  }

  // 如果 fast 不为 null，说明链表长度为奇数
  // 需要将 slow 再移动一位
  if (fast) slow = slow.next

  // 获取从中间隔开的两条对称的链表
  let left = head, right = reverseList(slow)

  // 依次遍历，直到中间
  while (right) {
    if (left.val !== right.val) return false
    left = left.next
    right = right.next
  }

  return true
};

// 翻转链表，返回翻转后的链表的首个结点
function reverseList(head) {
  if (!head || !head.next) return head

  let pre = null, cur = head, nxt = head.next

  while (cur) {
    cur.next = pre
    pre = cur
    cur = nxt
    if (nxt) nxt = nxt.next
  }

  return pre
};